/**
 * Created by Administrator on 2016/9/18.
 */
define(function (require, exports) {
    var ajaxUtil = require('util/ajaxUtil').ajaxUtil;
    var logUtil = require('util/logUtil').logUtil;
    var commonUtil = require('util/commonUtil').commonUtil;

    var admin_user_mgr_panel = $('#admin_user_mgr_panel');
    admin_user_mgr_panel.user_list_body = admin_user_mgr_panel.find('tbody:eq(0)');

    var admin_user_mgr_edit = $('#admin_user_mgr_edit');


    var urls = {};
    urls.admin_user_query = './?_m=/admin/admin_user/query';
    urls.admin_user_save = './?_m=/admin/admin_user/save';
    urls.admin_user_delete = './?_m=/admin/admin_user/delete';

    var G_USER_LIST_DATA = null;
    var G_USER_LIST_PAGE = 1;
    var G_USER_LIST_LIMIT = 10;


    function init() {
        bindEvent();
        genUserList(G_USER_LIST_PAGE);
    }

    function bindEvent() {
        admin_user_mgr_panel.find('button:eq(0)').bind('click',function () {
            $('#user_edit_node').html(template('tpl_user_edit'));
            showUserEdit();
        });

        admin_user_mgr_edit.find('button:eq(-1)').bind('click',function() {
            genUserList(G_USER_LIST_DATA);
            showUserList();
        });

        admin_user_mgr_edit.find('button:eq(-2)').bind('click',function() {
            doSaveAction();
        });

        admin_user_mgr_panel.user_list_body.on({'click':function() {
            var i=$(this).attr('aid');
            var data = G_USER_LIST_DATA[i];

            showUserEdit();
            $('#user_edit_node').html(template('tpl_user_edit',data));

            if(data.type==0) {
                $('#user_edit_node').find('input[type=checkbox]').each(function(){
                    $(this).prop('checked',true);
                    $(this).prop('disabled',true);
                });
            } else {
                if(data.rights) {
                    var rights = data.rights.split('|');
                    $('#user_edit_node').find('input[type=checkbox]').each(function(){
                        var _self = this;

                        rights.forEach(function(item){
                            if($(_self).val() == item) {
                                $(_self).prop('checked',true);
                            }
                        });
                    });
                }
            }

        }},'.user-edit');

        admin_user_mgr_panel.user_list_body.on({'click':function() {
            var i=$(this).attr('aid');
            var data = G_USER_LIST_DATA[i];

            layer.confirm('确定删除？', {
                btn: ['确定','取消'] //按钮
            }, function(){

                ajaxUtil.doAjaxGet(urls.admin_user_delete,{'id':data.id}).done(function (ret) {
                    if(ret.status == 0) {
                        layer.closeAll();
                        genUserList(G_USER_LIST_PAGE);
                    } else {
                        commonUtil.toast(ret.msg);
                    }
                });

            }, function(){
                layer.closeAll();
            });



        }},'.user-delete');

        admin_user_mgr_edit.on({'click':function() {
            var _self = this;
            $(_self).parent().parent().find('.second-level-checkbox').each(function() {
                $(this).find('input:eq(0)').prop('checked',$(_self).find('input:eq(0)').prop('checked'));
            });
        }},'.first-level-checkbox');

        $('#admin_user_pager').on('page',function(e,num){
            genUserList(num);
        })
    }

    function showUserList() {
        admin_user_mgr_panel.show();
        admin_user_mgr_edit.hide();
    }

    function showUserEdit() {
        admin_user_mgr_panel.hide();
        admin_user_mgr_edit.show();
    }

    function doSaveAction() {
        var account = $.trim(admin_user_mgr_edit.find('input[type=text]:eq(0)').val());
        var password = $.trim(admin_user_mgr_edit.find('input[type=text]:eq(1)').val());

        var rights = [];

        $('.second-level-checkbox').each(function() {
            if($(this).find('input:eq(0)').prop('checked')) {
                rights.push($(this).find('input:eq(0)').val());
            }
        });

        var args = {};
        args.account = account;
        args.password = password;
        args.rights = rights.join('|');

        var id = admin_user_mgr_edit.find('input[type=hidden]:eq(0)').val();

        if(id) {
            args.id=id;
        }
        
        ajaxUtil.doAjaxPost(urls.admin_user_save,args).done(function(ret) {
            commonUtil.toast(ret.msg);
            if(ret.status==0) {
                $('#user_edit_node').html(template('tpl_user_edit'));

                if(id) {
                    showUserList();
                    genUserList(G_USER_LIST_PAGE);
                }
            }
        });
    }

    function genUserList(index) {
        var args = {};

        args.page = index;
        args.limit = G_USER_LIST_LIMIT;

        ajaxUtil.doAjaxGet(urls.admin_user_query,args).done(function(ret) {
            if(ret.status == 0) {
                admin_user_mgr_panel.user_list_body.empty();

                G_USER_LIST_DATA = ret.data.page_data;
                G_USER_LIST_PAGE = index;

                if(ret.data.total <=0 ) {
                    commonUtil.toast('暂无数据');
                    $('#admin_user_pager').hide();
                } else {
                    G_USER_LIST_DATA.forEach(function(item,i) {
                        item.index = i;
                        admin_user_mgr_panel.user_list_body.append(template('tpl_user_list',item));
                    });

                    $('#admin_user_pager').show();
                    $('#admin_user_pager').bootpag({
                        total: ret.data.last_page,
                        page: G_USER_LIST_PAGE,
                        maxVisible: 5,
                        leaps: true,
                        firstLastUse: true,
                        first: '首页',
                        last: '末页',
                        next: '>>',
                        prev: '<<',
                        wrapClass: 'pagination',
                        activeClass: 'active',
                        disabledClass: 'disabled',
                        nextClass: 'next',
                        prevClass: 'prev',
                        lastClass: 'last',
                        firstClass: 'first'
                    });
                }
            } else {
                commonUtil.toast(ret.msg);
            }
        });
    }

    exports.init = init;
});